﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Domain;

namespace Mengsk.ProxyDelivery.App.UI.Accounting
{
    /// <summary>
    /// SellerMoneyHistoryUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class SellerTradeUserControl : UserControl, IPlugin
    {
        private bool myLoaded = false;
        private SellerTradeService service = ServiceContainer.GetService<SellerTradeService>();

        public string Title
        {
            get { return "交易记录"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        public SellerTradeUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.myLoaded)
            {
                return;
            }
            this.myLoaded = true;
            this.cbbTypes.Bind<SellerTradeType>();
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            int sellerId = 0;
            long orderId = 0;
            int.TryParse(this.tbSellerId.Text.Trim(), out sellerId);
            long.TryParse(this.tbOrderId.Text.Trim(), out orderId);
            this.pbBar.Parameters.Clear();
            this.pbBar.Parameters.Add("sellerId", sellerId);
            this.pbBar.Parameters.Add("type", this.cbbTypes.GetSelectedEnum<SellerTradeType>());
            this.pbBar.Parameters.Add("orderId", orderId);
            this.pbBar.Parameters.Add("externId", this.tbExternId.Text.Trim());
            this.pbBar.Parameters.Add("startTime", this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value);
            this.pbBar.Parameters.Add("endTime", this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value);
            this.pbBar.StartPage();
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var ret = this.service.GetByAll(e.GetParameter<int>("sellerId"), e.GetParameter<SellerTradeType>("type"), e.GetParameter<long>("orderId"), e.GetParameter<string>("externId"), e.GetParameter<DateTime>("startTime"), e.GetParameter<DateTime>("endTime"), "", e.CurrentPage - 1, e.PageSize);
            this.pbBar.Total = ret.Total;
            this.pbBar.CurrentCount = ret.Datas.Count;
            this.dgvItems.ItemsSource = ret.Datas;
        }

    }
}
